package com.gsdl.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;

import org.apache.log4j.Logger;

import com.gsdl.model.bo.DepositoBO;
import com.gsdl.model.bo.MaterialBO;
import com.gsdl.model.bo.StockBO;
import com.gsdl.model.delegate.BusinessDelegate;
import com.gsdl.model.entity.CategoriaMaterial;
import com.gsdl.model.entity.Deposito;
import com.gsdl.model.entity.Material;
import com.gsdl.model.entity.Movimento;
import com.gsdl.model.entity.Stock;
import com.gsdl.web.util.JsfUtil;


// TODO: Auto-generated Javadoc
/**
 * The Class MercadoriaController.
 *
 * @author Natacha Antonopoulos
 */

@ManagedBean
@ViewScoped
public class MercadoriaController implements Serializable{
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 7515050598241111152L;
	
	/** The Constant LOG. */
	private static final Logger LOG = Logger.getLogger(MercadoriaController.class.getName());
	
	/** The material. */
	private Material material = new Material();
	
	/** The novo material. */
	private Material novoMaterial = new Material();
	
	/** The materials. */
	private List<Material> materials =  new ArrayList<Material>();;
	
	/** The categorias material. */
	private List<CategoriaMaterial> categoriasMaterial = new ArrayList<CategoriaMaterial>();
	
	/** The stock list. */
	private List<Stock> stockList = new ArrayList<Stock>();
	
	/** The stock. */
	private Stock stock = new Stock();
	
	/** The movimentos. */
	private List<Movimento> movimentos = new ArrayList<Movimento>();
	
	/**
	 * Check if there is a materialId as a request parameter. If there is sets the material for details view
	 */
	@PostConstruct
	private void init() {
		String materialId = JsfUtil.getRequestParameter("materialId");
		if(materialId != null && !materialId.equals(""))
			material = BusinessDelegate.getInstance().getMaterialById(Long.parseLong(materialId));
	
	}
	
	
	/**
	 * Save new material action.
	 *
	 * @return the string
	 */
	public String saveNewMaterialAction(){
		try{
	
			CategoriaMaterial categoriaMaterial = novoMaterial.getCategoria();
			for(CategoriaMaterial cat: getCategoriasMaterial()){
				if(cat.getId().equals(categoriaMaterial.getId()))
					novoMaterial.setCategoria(cat);
			}
			
			novoMaterial = BusinessDelegate.getInstance().saveMaterial(novoMaterial, stockList);
			JsfUtil.addSuccessMessage("Material registado com sucesso");
			return "mercadorias";
		}catch (Exception ex) {
			JsfUtil.addErrorMessage("Erro ao registar material");
        }
		return "";
	}
	
	/**
	 * Save material action.
	 *
	 * @return the string
	 */
	public String saveMaterialAction(){
		try{
			material = BusinessDelegate.getInstance().saveMaterial(material, stockList);
			return "mercadorias";
		}catch (Exception ex) {
			JsfUtil.addErrorMessage("Erro ao registar material");
	    }
		return "";
	}
	
	/**
	 * Novo stock.
	 *
	 * @return the string
	 */
	public String novoStock(){
		
		Deposito deposito = BusinessDelegate.getInstance().getDepositoById(stock.getDeposito().getId());
		stock.setDeposito(deposito);

		stockList.add(stock);
		stock = new Stock();
		stock.setDeposito(new Deposito());
		return "";
	}
	
	/**
	 * Removes the stock.
	 *
	 * @param index the index
	 * @return the string
	 */
	public String removeStock(int index){
		stockList.remove(index);
		return "";
	}
	
	/**
	 * Novo material.
	 *
	 * @return the string
	 */
	public String novoMaterial(){
		novoMaterial = new Material();
		CategoriaMaterial categoriaMaterial = new CategoriaMaterial();
		novoMaterial.setCategoria(categoriaMaterial);
		stockList = new ArrayList<Stock>();
		stock = new Stock();
		stock.setDeposito(new Deposito());
		return "novoMaterial";
	}
	
	/**
	 * Go to material details.
	 *
	 * @param mat the mat
	 * @return the string
	 */
	public String goToMaterialDetails(Material mat){
		this.material = mat;//MaterialFacade.getInstance().getById(mat.getId());
		stockList = BusinessDelegate.getInstance().getStockMaterial(material);
		stock = new Stock();
		stock.setDeposito(new Deposito());
		return "material";
	}
	
	/**
	 * Removes the material.
	 *
	 * @param material the material
	 * @return the string
	 */
	public String removeMaterial(Material material){
		BusinessDelegate.getInstance().removeMaterial(material);
		getMaterials();
		return "";
	}
	
	/**
	 * Gets the material.
	 *
	 * @return the material
	 */
	public Material getMaterial() {
		return material;
	}

	/**
	 * Sets the material.
	 *
	 * @param material the new material
	 */
	public void setMaterial(Material material) {
		this.material = material;
	}

	/**
	 * Gets the materials.
	 *
	 * @return the materials
	 */
	public List<Material> getMaterials() {
		materials = BusinessDelegate.getInstance().getAllMaterial();
		return materials;
	}

	/**
	 * Sets the materials.
	 *
	 * @param materials the new materials
	 */
	public void setMaterials(List<Material> materials) {
		this.materials = materials;
	}

	/**
	 * Gets the novo material.
	 *
	 * @return the novoMaterial
	 */
	public Material getNovoMaterial() {
		return novoMaterial;
	}

	/**
	 * Sets the novo material.
	 *
	 * @param novoMaterial the novoMaterial to set
	 */
	public void setNovoMaterial(Material novoMaterial) {
		this.novoMaterial = novoMaterial;
	}

	/**
	 * Gets the categorias material.
	 *
	 * @return the tiposMaterial
	 */
	public List<CategoriaMaterial> getCategoriasMaterial() {
		if(categoriasMaterial == null || categoriasMaterial.isEmpty())
			categoriasMaterial = BusinessDelegate.getInstance().getCategoriasMaterial();
		return categoriasMaterial;
	}

	/**
	 * Sets the categorias material.
	 *
	 * @param categoriasMaterial the new categorias material
	 */
	public void setCategoriasMaterial(List<CategoriaMaterial> categoriasMaterial) {
		this.categoriasMaterial = categoriasMaterial;
	}

	/**
	 * Gets the stock list.
	 *
	 * @return the stockList
	 */
	public List<Stock> getStockList() {
		return stockList;
	}

	/**
	 * Sets the stock list.
	 *
	 * @param stockList the stockList to set
	 */
	public void setStockList(List<Stock> stockList) {
		this.stockList = stockList;
	}

	/**
	 * Gets the stock.
	 *
	 * @return the stock
	 */
	public Stock getStock() {
		return stock;
	}

	/**
	 * Sets the stock.
	 *
	 * @param stock the stock to set
	 */
	public void setStock(Stock stock) {
		this.stock = stock;
	}

	/**
	 * Gets the movimentos.
	 *
	 * @return the movimentos
	 */
	public List<Movimento> getMovimentos() {
		this.movimentos = BusinessDelegate.getInstance().getMovimentosByMaterial(material);
		return movimentos;
	}

	/**
	 * Sets the movimentos.
	 *
	 * @param movimentos the movimentos to set
	 */
	public void setMovimentos(List<Movimento> movimentos) {
		this.movimentos = movimentos;
	}
}
